package RestTest;

import java.io.IOException;
import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: java_learning
 * @description 成神之路 自动拆装箱练习
 * @author: 辛旭初
 * @create: 2021-01-07 11:33
 **/
public class test20210107 {

    public static void integerCache() {
        Integer integer1 = 3;
        Integer integer2 = 3;

        if (integer1 == integer2)
            System.out.println("integer1 == integer2");
        else
            System.out.println("integer1 != integer2");

        Integer integer3 = 128;
        Integer integer4 = 128;

        if (integer3 == integer4)
            System.out.println("integer3 == integer4");
        else
            System.out.println("integer3 != integer4");
    }


    public static void main(String[] args) {
//        integerCache();
//        test_replaceAll();
//        test_replaceFirst();
//        test_replaceAll1();
        System.out.println("abac".replace("a", "\\a")); //\ab\ac
    }


    public static void test_replaceAll() {

        //文字替换（全部）
        Pattern pattern = Pattern.compile("正则表达式");
        Matcher matcher = pattern.matcher("正则表达式 Hello World,正则表达式 Hello World");
        //替换所有符合正则的数据
        System.out.println(matcher.replaceAll("Java"));
    }

    public static void test_replaceFirst(){
        //去除html标记
        Pattern pattern = Pattern.compile("<.+?>", Pattern.DOTALL);
        Matcher matcher = pattern.matcher("<a href=\"index.html\">主页</a>");
        String string = matcher.replaceAll("");
        System.out.println(string);
    }

    public static void test_replaceAll1(){
        //替换指定{}中文字
        String str = "Java目前的发展史是由{0}年-{1}年";
        String[][] object = {
                new String[] {
                        "\\{0\\}",
                        "1995"
                },
                new String[] {
                        "\\{1\\}",
                        "2007"
                }
        };
        System.out.println(replace(str, object));
    }

    public static String replace(final String sourceString, Object[] object) {
        String temp = sourceString;
        for (int i = 0; i < object.length; i++) {
            String[] result = (String[]) object[i];
            Pattern pattern = Pattern.compile(result[0]);
            Matcher matcher = pattern.matcher(temp);
            temp = matcher.replaceAll(result[1]);
        }
        return temp;
    }
}
